//
//  MainViewController.m
//  OpenGLExample
//
//  Copyright 2010 Google Inc. All rights reserved.
//

#import "GADMasterViewController.h"
#import "EAGLView.h"


@implementation GADMasterViewController

@synthesize adViewController;
@synthesize glView;

- (void)viewDidLoad {
  [super viewDidLoad];
  id<GADMasterViewControllerDelegate> delegate =
      (id<GADMasterViewControllerDelegate>)[[UIApplication sharedApplication]
                                           delegate];
  self.adViewController = [delegate getAdViewController];
  [self.adViewController setDelegate:self];

  self.adViewController.adSize = kGADAdSize320x50;
  NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"Company Name", kGADAdSenseCompanyName,
                              @"App Name", kGADAdSenseAppName,
                              @"0", kGADAdSenseApplicationAppleID,
                              @"hotels+san+francisco", kGADAdSenseKeywords,
                              [NSArray arrayWithObjects:@"06562723305", nil], kGADAdSenseChannelIDs,
                              [NSNumber numberWithInt:1], kGADAdSenseIsTestAdRequest,
                              nil];
  [self.adViewController loadGoogleAd:attributes];
  NSLog(@"Showing new ad");
  UIView *adView = self.adViewController.view;
  CGPoint center = CGPointMake(160, 455);
  [adView setCenter:center];
  adView.center = center;

  [self.view insertSubview:adView atIndex:0];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  // Return YES for supported orientations
  return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
  // Releases the view if it doesn't have a superview.
  [super didReceiveMemoryWarning];

  // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
  [super viewDidUnload];
  // Release any retained subviews of the main view.
  // e.g. self.myOutlet = nil;
}

- (void)dealloc {
  [self.adViewController setDelegate:nil];
  self.adViewController = nil;
  self.glView = nil;
  [super dealloc];
}

- (UIViewController *)viewControllerForModalPresentation:
    (GADAdViewController *)adController {
  return self;
}

- (GADAdClickAction)adControllerActionModelForAdClick:
(GADAdViewController *)adController {
  NSLog(@"Stopping animation");
  [glView stopAnimation];
  return GAD_ACTION_DISPLAY_INTERNAL_WEBSITE_VIEW;
}

- (void)adControllerDidCloseWebsiteView:(GADAdViewController *)adController {
  NSLog(@"Starting animation");
  [glView startAnimation];
}


@end
